package com.pinyougou.sellergoods.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.pojo.TbBrandExample.Criteria;
import com.pinyougou.sellergoods.service.BrandService;

import entity.PageResult;

@Service
public class BrandServiceImpl implements BrandService {
	@Autowired
    private TbBrandMapper tbm;
	private TbBrandExample tbBrandExample;
	private Criteria createCriteria;

	@Override
	public List<TbBrand> findAll() {
		// TODO Auto-generated method stub
		List<TbBrand> tbBrandList = tbm.selectByExample(null);
		return tbBrandList;
	}
	@Override
	public PageResult findByPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		Page<TbBrand> pageList = (Page)tbm.selectByExample(null);
		
		return new PageResult(pageList.getTotal(),pageList.getResult());
	}
	@Override
	public void addBrand(TbBrand brand) {
		// TODO Auto-generated method stub
		tbm.insert(brand);
		
	}
	@Override
	public TbBrand findById(long id) {
		TbBrand tbBrand = tbm.selectByPrimaryKey(id);
		// TODO Auto-generated method stub
		return tbBrand;
	}
	@Override
	public void update(TbBrand brand) {
		// TODO Auto-generated method stub
		tbm.updateByPrimaryKeySelective(brand);
	}
	@Override
	public void deleteById(long[] id) {
		// TODO Auto-generated method stub
		for (long beandId : id) {
			tbm.deleteByPrimaryKey(beandId);
		}
	}
	@Override
	public PageResult search(TbBrand brand, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		tbBrandExample = new TbBrandExample();
		createCriteria = tbBrandExample.createCriteria();
		if (brand != null) {
			if (brand.getName() != null && brand.getName().length() > 0) {
				createCriteria.andNameLike("%"+brand.getName()+"%");
			}
			if (brand.getFirstChar() != null && brand.getFirstChar().length()>0) {
				createCriteria.andFirstCharLike("%"+brand.getFirstChar()+"%");
			}
		}
		Page<TbBrand> page = (Page<TbBrand>)tbm.selectByExample(tbBrandExample);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<Map> selectOptionList() {//返回下拉列表
		return tbm.selectOptionList();
	}


}
